package com.hongyuan.sellfitness.ui.person.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.ui.find.beans.FileBean;
import com.hongyuan.sellfitness.ui.main.beans.ReturnImgsBean;
import com.hongyuan.sellfitness.ui.person.beans.PersonBean;

public class EditInforMationViewModel extends CustomViewModel<MyRepository> {

    public ObservableField<PersonBean.InfoBean> entity = new ObservableField<>();

    public EditInforMationViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //打开相册
        public SingleLiveEvent<Void> openPhoto = new SingleLiveEvent<>();
        //修改昵称
        public SingleLiveEvent<Void> changeNike = new SingleLiveEvent<>();
        //修改性别
        public SingleLiveEvent<Void> changeSex = new SingleLiveEvent<>();
        //修改生日
        public SingleLiveEvent<Void> changeBirth = new SingleLiveEvent<>();
        //修改真实姓名
        public SingleLiveEvent<Void> changeReName = new SingleLiveEvent<>();
        //修改个性签名
        public SingleLiveEvent<Void> changeSign = new SingleLiveEvent<>();
    }

    //换头像
    public BindingCommand changeHead = new BindingCommand(() -> {
        uc.openPhoto.call();
    });

    //修改昵称
    public BindingCommand changeNike = new BindingCommand(() -> {
        uc.changeNike.call();
    });

    //修改性别
    public BindingCommand changeSex = new BindingCommand(() -> {
        uc.changeSex.call();
    });

    //修改生日
    public BindingCommand changeBirth = new BindingCommand(() -> {
        uc.changeBirth.call();
    });

    //修改真实姓名
    public BindingCommand changeReName = new BindingCommand(() -> {
        if(!BaseUtil.isValue(entity.get().getMi_realname())){
            uc.changeReName.call();
        }else {
            ToastUtils.showShort("不可二次修改");
        }

    });

    //修改个性签名
    public BindingCommand changeSign = new BindingCommand(() -> {
        uc.changeSign.call();
    });


    /*
    * 单文件上传
    * */
    public void updateFile(FileBean bean){
        setParams("type","1").setFileParams(bean);

        requestData(Constants.UPFILE_OSS);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MEMBER_INDEX_INFO){
            PersonBean.InfoBean infoBean = ((PersonBean)dataBean).getInfo();
            entity.set(infoBean);
        }

        if(code == Constants.UPFILE_OSS){
            ReturnImgsBean imgsBean = (ReturnImgsBean)dataBean;

            //修改用户头像
            clearParams().setParams("head_src",imgsBean.getFile_url());
            requestData(Constants.UPDATE_MEMBER_HEAD);
            entity.get().setMi_head(imgsBean.getFile_url());
        }

    }
}
